package priv.yiyuan.example.design.chain;

import java.util.UUID;

/**
 * 生成Token
 *
 * @author YiYuan
 * @data 2025/10/11
 * @apoNote
 */
public class TokenGenerationCommand extends AbstractLoginCommand {

    @Override
    protected boolean process(LoginContext context) {

        if (context.getTenantId() == null || context.getTenantId().isEmpty()) {
            context.setErrorMessage("未获取到租户信息。");
            return false;
        }

        System.out.println("正在为用户 [" + context.getUsername() + "] 和租户 [" + context.getTenantId() + "] 生成Token...");
        String rawToken = context.getUsername() + ":" + context.getTenantId() + ":" + UUID.randomUUID().toString();
        String token = "JWT_" + rawToken.hashCode();

        context.setToken(token);
        context.setSuccess(true);
        System.out.println("√ Token生成成功。");
        return true;
    }
}
